package com.study.samsung.Control;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.study.samsung.Entity.Commodity;
import com.study.samsung.Entity.Succession;
import com.study.samsung.Entity.User;
import com.study.samsung.Service.GoodsListService;
import com.study.samsung.Service.GoodsService;
import com.study.samsung.Service.IndexSearchService;
import com.study.samsung.Utils.BaseServlet;
import com.study.samsung.Utils.Pager;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.PrintWriter;
import java.util.List;

@WebServlet("/goodsList")
public class GoodsListServlet extends BaseServlet {
    GoodsListService gls = new GoodsListService();
    IndexSearchService iss = new IndexSearchService();
    GoodsService gds = new GoodsService();

    @Override
    public String excute(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        List<Succession> selectxl1 = gls.selectxl();
        request.setAttribute("selectxl1",selectxl1);
        String show = request.getParameter("show");

        Integer xid;

        Integer pageNo = 1,pageSize = 16;
        String xid1 = request.getParameter("xid");
        if (xid1==null || show==null){
            return "r:index";
        }else {
            xid = Integer.parseInt(xid1);
        }
        Pager pager = gls.selectdata(xid,pageNo,pageSize);

        if (show!=null){
            request.setAttribute("pager",pager);
            request.setAttribute("show",show);
            request.setAttribute("id",xid);
        }

        List<Commodity> newgoods = iss.selectnew();
        List<Succession> selectxl = iss.selectxl();

        request.setAttribute("newgoods",newgoods);
        request.setAttribute("selectxl",selectxl);

        Object user = session.getAttribute("user");
        if (user!=null){
            User nowuser = (User) user;
            Integer uid = nowuser.getUid();

            session.setAttribute("cartcount",gds.cartcount(uid));
        }
        return "f:goodsList";
    }
    public void xlfy(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String pageNo = request.getParameter("pageNo");
        String arr = request.getParameter("arr");
        PrintWriter out = response.getWriter();
        JSONObject jsonObject=new JSONObject();
        if (pageNo==null){
            pageNo="1";
        }
        if (arr!=null){
            JSONArray objects = JSON.parseArray(arr);
            String[] strings=new String[objects.size()];
            for (int i=0;i<strings.length;i++){
                strings[i]=objects.get(i).toString();
            }
            Pager flquery = gls.flquery(strings, Integer.parseInt(pageNo));
            jsonObject.put("pager",flquery);
        }else {
            String xid = request.getParameter("xid");
            if (xid!=null){
                Pager pager=new Pager(Integer.parseInt(pageNo),16);
                pager.setTotal(gls.getTotal(xid,Integer.parseInt(pageNo)));
                jsonObject.put("pager",pager);
            }
        }
        out.println(jsonObject);
    }

//    public void paixu(HttpServletRequest request, HttpServletResponse response) throws Exception {
//        String pageNo = request.getParameter("pageNo");
//        String arr = request.getParameter("arr");
//        PrintWriter out = response.getWriter();
//        JSONObject jsonObject=new JSONObject();
//
//        JSONArray array = JSON.parseArray(arr);
//        String[] strings = new String[array.size()];
//        for (int i = 0; i < strings.length; i++) {
//            strings[i]=array.get(i).toString();
//        }
//        Pager pxquery = gls.pxquery(strings, Integer.parseInt(pageNo));
//        jsonObject.put("pager",pxquery);
//        out.println(jsonObject);
//    }

    public void shaixuan(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        String pageNo = request.getParameter("pageNo");
        String arr = request.getParameter("arr");
        String lsprice = request.getParameter("lsprice");
        String rsprice = request.getParameter("rsprice");

        Integer leftspc = null,rightspc = null;
        int pageno = Integer.parseInt(pageNo);
        if (lsprice!=""){
            leftspc = Integer.parseInt(lsprice);
        }
        if (rsprice!=""){
            rightspc = Integer.parseInt(rsprice);
        }
        PrintWriter out = response.getWriter();
        JSONObject jsonObject=new JSONObject();

        JSONArray array = JSON.parseArray(arr);
        String[] strings = new String[array.size()];
        for (int i = 0; i < strings.length; i++) {
            strings[i]=array.get(i).toString();
        }

        Pager shaixuan = gls.shaixuan(strings, pageno, leftspc, rightspc);
        jsonObject.put("pager",shaixuan);
        out.println(jsonObject);
    }
}
